/*
 * Task 4. Calendar seeker
 */
import java.util.*;
import java.io.*;

/**
 * Class for retrieving information from calendar
 * 
 * @author Vlad Oblamskyi
 */
public class CalendarSeeker {
    private static final String[] WEEK_DAYS = {"Sunday", "Monday", "Tuesday",
                                               "Wednesday", "Thursday",
                                               "Friday", "Saturday"};
    private static final int[] TONDERING_MONTH_VALUES = {0, 3, 2, 5, 0, 3, 
                                                         5, 1, 4, 6, 2, 4};  
    private static final int DAYS_IN_WEEK = 7;                                                         
    private static final int LEAP_DIVIDER_ONE = 4;
    private static final int LEAP_DIVIDER_TWO = 100;
    private static final int LEAP_DIVIDER_THREE = 400;
    private static final int TONDERING_BOUND_MONTH_INDEX = 3;
       
    public static int getWeekDayIndex(int year, int month, int day) {
        /*
         * Implementing Tondering's algorithm (Tomohiko Sakamoto implementation)
         * http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week
         */
        year -= (month < TONDERING_BOUND_MONTH_INDEX) ? 1 : 0;
        
        /* 
         * Month varies from 1 to 12, so to get corresponding value from Tondering
         * month values we must use (month - 1) as index
         */
        return (year + year / LEAP_DIVIDER_ONE - year / LEAP_DIVIDER_TWO 
                + year / LEAP_DIVIDER_THREE + TONDERING_MONTH_VALUES[month - 1] + day)
               % DAYS_IN_WEEK;
    }
    
    public static int getWeekDayIndex(Date date) {
        return getWeekDayIndex(date.getYear(), date.getMonth(), date.getDate());
    }
    
    public static String getDayName(int dayIndex) {
        return WEEK_DAYS[dayIndex];
    }
    
    public static Date getInput(Scanner in) {
        System.out.print("Enter day: ");
        int day = in.nextInt();
        System.out.print("Enter month: ");
        int month = in.nextInt();
        System.out.print("Enter year: ");
        int year = in.nextInt();
                
        return new Date(year, month, day);
    }
    
    public static void printResult(String result) {
        System.out.println("Result: " + result);
    }
       
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        
        Date date = getInput(in);
        
        int dayIndex = getWeekDayIndex(date);
        String dayName = getDayName(dayIndex);
        
        printResult(dayName);
        
        in.close();
    }    
}
